package org.ailoli.aixiang.widgets

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.icu.math.BigDecimal
import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RequiresApi
import org.ailoli.aixiang.R


class BatteryView :
    View {

    private var abnormal: Boolean = false
    private var mBatteryPait: Paint? = null
    private var mPowerPaint: Paint? = null
    private val mBatteryStroke = 10.0f

    /**
     * 屏幕的高宽
     *
     * @param context
     */
    private var measureWidth = 0
    private var measureHeight = 0

    /**
     * 电池参数
     *
     * @param context
     */
    private var mBatteryHeight = 30.0f // 电池高度

    private var mBatteryWidth = 60.0f // 电池的宽度

    private var mCapHeight = 15.0f
    private var mCapWidth = 5.0f

    /**
     * 电池电量
     *
     * @param context
     */
    private val mPowerPadding = 5.0f
    private val mPowerHeight = (mBatteryHeight - mBatteryStroke
            - mPowerPadding)
    private val mPowerWidth = mBatteryWidth - mBatteryStroke - (mPowerPadding
            * 2) // 电池身体的总宽度

    private val mPower = 0f

    private var batteryNums = 0F //电池电量

    /**
     * 矩形
     */
    private var mBatteryRectF: RectF? = null
    private var mCapRectF: RectF? = null
    private var mPowerRectF: RectF? = null

    constructor(
        context: Context?,
        attrs: AttributeSet?
    ) : super(context, attrs) {
        getAttrFormXml(context!!, attrs!!)
    }

    @SuppressLint("CustomViewStyleable", "Recycle")
    private fun getAttrFormXml(context: Context, attrs: AttributeSet) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Battery)
//        val width = typedArray.getInt(R.styleable.ConstraintSet_android_layout_width, 15)
        this.batteryNums = typedArray.getFloat(R.styleable.Battery_battery, 0F)
        val test = ""
    }

    public fun setBatteryStatus(abnormal: Boolean) {
        this.abnormal = abnormal
        invalidate()
        requestLayout()
    }

    public fun setBatterySoc(soc: Int) {
        this.batteryNums = soc.toFloat()
        invalidate()
        requestLayout()
    }

    @SuppressLint("ResourceAsColor")
    private fun initView() {
        /**
         * 设置电池画笔
         */
        mBatteryPait = Paint()
        mBatteryPait!!.color = Color.WHITE
        mBatteryPait!!.strokeWidth = mBatteryStroke
        mBatteryPait!!.style = Paint.Style.STROKE
        mBatteryPait!!.isAntiAlias = true
        /**
         * 电量画笔
         */
        mPowerPaint = Paint()
//        mPowerPaint!!.color = if (abnormal) Color.RED else Color.GREEN
        mPowerPaint!!.color = if (abnormal) Color.RED else if (batteryNums < 80) resources.getColor(R.color.colorBatteryOrange) else resources.getColor(R.color.colorBatteryGreen)
        mPowerPaint!!.style = Paint.Style.FILL
        mPowerPaint!!.strokeWidth = mBatteryStroke
        mPowerPaint!!.isAntiAlias = true
        /**
         * 设置电池矩形
         */
        mBatteryRectF = RectF(
            mBatteryStroke / 2,
            mBatteryStroke / 2,
            mBatteryWidth - mCapWidth / 2 - mBatteryStroke,
            mBatteryHeight - mBatteryStroke / 2
        )
//        mBatteryText =
        /**
         * 设置电池盖矩形
         */
        mCapRectF = RectF(
            mBatteryWidth - mCapWidth / 2 - mBatteryStroke,
            mBatteryHeight / 2 - mCapHeight / 2,
            mBatteryWidth + mCapWidth / 2,
            mBatteryHeight / 2 + mCapHeight / 2
        )
        /**
         * 设置电量的矩形
         */
        val batteryInWidth = mBatteryWidth - mCapWidth - mBatteryStroke
        val right = (mBatteryWidth  - mBatteryStroke - mCapWidth) * (this.batteryNums / 100)
        mPowerRectF = RectF(
            mBatteryStroke,
            mBatteryStroke,
//            1F,
            if (right <= mBatteryStroke) mBatteryStroke + right else right,
            mBatteryHeight - mBatteryStroke
        )
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        measureHeight = MeasureSpec.getSize(heightMeasureSpec)
        measureWidth = MeasureSpec.getSize(widthMeasureSpec)
        this.mCapHeight = measureHeight.toFloat() / 2
        this.mCapWidth = measureWidth.toFloat() / 20
        this.mBatteryHeight = measureHeight.toFloat()
        this.mBatteryWidth = measureWidth.toFloat() - this.mCapWidth
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec)
        initView()
    }

    @RequiresApi(Build.VERSION_CODES.O)
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.save()
        var batteryStr = this.batteryNums.toInt().toString() + "%"
        if (this.abnormal) {
            batteryStr = "升级中"
        }
        canvas.drawRoundRect(mBatteryRectF!!, 2.0f, 2.0f, mBatteryPait!!)
        canvas.drawRoundRect(mCapRectF!!, 2.0F, 2.0F, mBatteryPait!!)
        canvas.drawRect(mPowerRectF!!, mPowerPaint!!)
        val textPaint = Paint()
//        val batteryStr = this.batteryNums.toString() + "%"
        textPaint.textSize = 32F
//        textPaint.setTypeface(Typeface.createFromAsset(context.assets, "fonts/helvetica.ttf"))
        textPaint.setColor(Color.WHITE)
        textPaint.strokeWidth = 5F
        textPaint.textAlign = Paint.Align.CENTER
        textPaint.setAntiAlias(true)
        textPaint.setFilterBitmap(true)
        val textRect = Rect()
        textPaint.getTextBounds(batteryStr, 0, batteryStr.length, textRect)
        canvas.drawText(batteryStr, mBatteryWidth / 2, (mBatteryHeight / 2) + mBatteryStroke, textPaint)
        canvas.restore()
    }
}